home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_03_03
/
3n03043a
< prev
next >
Wrap
Text File
|
1992-01-27
|
14KB
|
482 lines
// Listing 1 (video.c)
#include <windows.h>
#include "avkapi.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
//-----------------------------------------------------
// Misc constants
//-----------------------------------------------------
#define BOARD_NUM 0 // AVK board number
#define STREAM_XRES 256 // X Resolution of Stream
#define STREAM_YRES 240 // Y Resolution of Stream
#define VIEW_XRES 256 // X Resolution of View
#define VIEW_YRES 240 // Y Resolution of View
#define STREAM_CT 1 // Number of streams in buffer
#define STREAM_INDEX 0 // Index of stream in buffer
#define BUF_SIZE 65536L // Size of AVK buffer
#define EMPTY_LEVEL 32768L // Level which triggers
// buffer empty message
// Macro to check AVK error returns and report errors
#define RETURN_IF_AVK_ERROR(ret) \
if (ret != AVK_ERR_OK) \
{ \
ReportAvkError(ret, __LINE__); \
return ret; \
}
//-----------------------------------------------------
// Function prototypes
//-----------------------------------------------------
long FAR PASCAL WndProc(HWND, WORD, WORD, LONG);
int InitAvk(HWND hwnd);
int CreateView(void);
int CreateGroup(void);
int CreateConnectors(void);
int TurnOnAudio(void);
int FormatVideoStream(void);
int LoadAlgData(void);
void GuiRectToView(RECT *GuiRect, RECT *ViewRect);
void StopAvk(void);
int EnableConnector(HAVK ConnHandle);
int DisableConnector(HAVK ConnHandle);
int MoveConnector(HWND hwnd);
void GetAbsoluteClientRect(HWND hWnd, RECT *pRect);
void ReportAvkError(int Error, int LineNo);
BOX *RectToBox(RECT *pRect, BOX *pBox);
//-----------------------------------------------------
// Module global data
//-----------------------------------------------------
HANDLE hInstance;
HAVK hAvkSess; // AVK session handle
HAVK hDev; // AVK Device handle
HAVK hView; // AVK View handle
HAVK hGrp; // AVK Group handle
HAVK hGrpBuf; // AVK Group Buffer handle
HAVK hVidStream; // AVK Video Stream handle
HAVK hDigiToStrmConn; // AVK Digitizer to Stream
// connector handle
HAVK hStrmToViewConn; // AVK Stream to View
// connector handle
int GuiXRes; // X Resolution of Windows screen
int GuiYRes; // Y Resolution of Windows screen
void *pVideoAlgData; // Ptr to Video Algorithm data
unsigned int LenVideoAlgData;
//-----------------------------------------------------
// WinMain
//-----------------------------------------------------
int PASCAL WinMain(HANDLE hInst, HANDLE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
char szAppName[] = "Live Video";
HWND hwnd;
MSG msg;
WNDCLASS wc;
hInstance = hInst;
if (!hPrevInstance)
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
RegisterClass(&wc);
}
else
{
MessageBox(GetFocus(),
"Live Video is already active", NULL, MB_OK);
return 0;
}
GuiXRes = GetSystemMetrics(SM_CXSCREEN);
GuiYRes = GetSystemMetrics(SM_CYSCREEN);
hwnd = CreateWindow(szAppName, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL, // No Parent
NULL, // No menu
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//-----------------------------------------------------
long FAR PASCAL WndProc(HWND hwnd, WORD message,
WORD wParam, LONG lParam)
{
PAINTSTRUCT ps;
// If the message is from AVK, ignore it
if (message >= AVK_MSG_LOW && message <= AVK_MSG_HIGH)
return(0);
switch(message)
{
case WM_CREATE:
if (InitAvk(hwnd) != AVK_ERR_OK ||
CreateView() != AVK_ERR_OK ||
CreateGroup() != AVK_ERR_OK ||
CreateConnectors() != AVK_ERR_OK ||
TurnOnAudio() != AVK_ERR_OK)
DestroyWindow(hwnd);
return 0;
case WM_PAINT:
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
case WM_MOVE:
case WM_SIZE:
MoveConnector(hwnd);
InvalidateRect(hwnd, NULL, 1);
break;
case WM_SHOWWINDOW:
EnableConnector(hDigiToStrmConn);
EnableConnector(hStrmToViewConn);
break;
case WM_DESTROY:
// Free algorithm data if allocated.
if (pVideoAlgData)
free(pVideoAlgData);
StopAvk();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
//-----------------------------------------------------
// Open an AVK session and device (board)
//-----------------------------------------------------
int InitAvk(HWND hwnd)
{
int rv;
rv = AvkBeginMsg(hwnd, &hAvkSess, AVK_SESSION_DEFAULT);
RETURN_IF_AVK_ERROR(rv);
rv = AvkDeviceOpen(hAvkSess, BOARD_NUM,
AVK_DEV_OPEN_EXCLUSIVE, &hDev);
RETURN_IF_AVK_ERROR(rv);
return AVK_ERR_OK;
}
//-----------------------------------------------------
// Create and display an AVK View
//-----------------------------------------------------
int CreateView(void)
{
int rv;
rv = AvkViewCreate(hDev, VIEW_XRES, VIEW_YRES, AVK_YUV9,
AVK_VID_VGA_KEYED, &hView);
RETURN_IF_AVK_ERROR(rv);
rv = AvkViewDisplay(hDev, hView,
AVK_TIME_IMMEDIATE, AVK_VIEW_DISPLAY_DEFAULT);
RETURN_IF_AVK_ERROR(rv);
return AVK_ERR_OK;
}
//-----------------------------------------------------
// Create AVK Group, Buffer, and Stream
//-----------------------------------------------------
int CreateGroup(void)
{
int rv;
rv = AvkGrpCreate(hDev, &hGrp);
RETURN_IF_AVK_ERROR(rv);
rv = AvkGrpBufCreate(hGrp, AVK_BUF_CAPTURE, BUF_SIZE,
EMPTY_LEVEL, STREAM_CT, &hGrpBuf);
RETURN_IF_AVK_ERROR(rv);
rv = AvkVidStrmCreate(hGrpBuf, STREAM_INDEX, &hVidStream);
RETURN_IF_AVK_ERROR(rv);
rv = FormatVideoStream();
return rv;
}
//-----------------------------------------------------
// Create all required connectors
//-----------------------------------------------------
int CreateConnectors(void)
{
int rv;
BOX Box;
Box.x1 = Box.y1 = 0;
Box.x2 = STREAM_XRES - 1;
Box.y2 = STREAM_YRES - 1;
rv = AvkConnCreate(AVK_CONN_DIGITIZER, NULL, hVidStream,
NULL, 0, &hDigiToStrmConn);
RETURN_IF_AVK_ERROR(rv);
rv = AvkConnCreate(hVidStream, NULL, hView, NULL,
AVK_PRE_MONITOR, &hStrmToViewConn);
RETURN_IF_AVK_ERROR(rv);
return AVK_ERR_OK;
}
//-----------------------------------------------------
// Turn on Audio monitoring
//-----------------------------------------------------
int TurnOnAudio(void)
{
int rv;
rv = AvkDeviceAudioIn(hDev, AVK_CD_LINE, AVK_MONITOR_ON);
RETURN_IF_AVK_ERROR(rv);
return AVK_ERR_OK;
}
//-----------------------------------------------------
// Format a video stream
//-----------------------------------------------------
int FormatVideoStream(void)
{
int rv;
AVK_RTV_20_ENCODE_ARGS RtvArgs = AVK_RTV_20_DEFAULT_ARGS;
// Fetch parameter data for video algorithm
if (!LoadAlgData())
return -1;
// Build appropriate data structure for algorithm
// Use the defaults from the initialization for most fields
RtvArgs.xLen = STREAM_XRES;
RtvArgs.yLen = STREAM_YRES;
RtvArgs.Flags = AVK_RTV_20_PREFILTER | AVK_RTV_20_